<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BScreenAppStore
 *
 * @author I062261
 */
class BScreenAppStore extends BUIScreen {

    //put your code here

    public function onRender($data = null) {
        //parent::render($data);
        $model = new AddAppFormModel();
        /*
          $model = new AddAppFormModel();

          if (isset($_POST['AddAppFormModel'])) {
          $model->attributes = $_POST['AddAppFormModel'];
          }

          $apps = $this->getAppsData();
          foreach ($apps as $id => $name) {
          if ($this->isAjaxLinkClicked(array('ajaxLinkId' => 'addApp' . $id))) {
          Blyn::app()->getActiveSpace()->addApp($id);
          //$this->addApps(array($id));
          // Blyn::app()->getUIManager()->gotoNode(Blyn::app()->getActiveSpace());
          }
          }
         * 
         */
//        $appStoreStr = Yii::t('site','appStore');
//        $this->addRenderView($appStoreStr, 'appStoreView', array('model' => $model));
        $this->addRenderView('appStore', 'appStoreView', array('model' => $model));
    }

    public function getAppsData() {

        $blynConfigPath = yii::getPathOfAlias('application.application.config') . '/config.php';

        $blynConfig = require $blynConfigPath;

//        $appStoreStr = Yii::t('site','appStore');
//        $configApps = $blynConfig[$appStoreStr]['app_list'];
        $configApps = $blynConfig['appStore']['app_list'];

        $apps = array();
        foreach ($configApps as $appClass => $appConfig) {
            if (class_exists($appClass)) {
                $app = new $appClass();
//                $appGroupRoot = BAppGroup::getRoot();
//                $appTypeGroup = BAppGroup::find('app_type');
//                if(!$appTypeGroup instanceof BAppGroup)
//                    $appTypeGroup = new BAppGroup ();
//                $appTypeGroup->name = 'app_type';
//                $appGroupRoot->addChild($appTypeGroup);
//                
//                $normalApp = BAppGroup::find('app_type_normal');
//                if(!$normalApp instanceof BAppGroup)
//                {
//                    $normalApp = new BAppGroup();
//                $normalApp->name = 'app_type_normal';
//                $appTypeGroup->addChild($normalApp);
//                }
//                $normalApp->addMember($app);

                $apps[$app->getId()] = $app;
            }
        }

        //$space = Blyn::app()->getActiveSpace();
        //$db = Blyn::app()->getAppDb();
        //$apps = $db->getVisibleApps($space);
        $appContext = new BApp();
        $appContext->name = BEntity::String_Any;
        $appContext->className = BEntity::String_Any;

        //$allAppGroup = BAppGroup::find($appContext);
//        $apps = BApp::findAll($appContext);

        $myApps = Blyn::app()->getActiveSpace()->getApps();

        $ids = array();

        foreach ($apps as $id => $app) {
            array_push($ids, $id);
        }

        foreach ($myApps as $app) {
            $appId = $app->getId();
//            $parentClass = get_parent_class($app);
//            if($app->getGroup('app_type.app_type_normal') instanceof BAppGroup) {
            if (in_array($appId, $ids)) {
                unset($apps[$appId]);
//                }
            }
        }

        $data = array();

        foreach ($apps as $app) {
            $appName = get_class($app);
            if ($appName != 'BPersonalApp') {
                $data[$app->getId()] = $app->getName();
            }
        }


        return $data;
    }

    public function addApps($sApps) {
        $spaceId = Blyn::app()->getActiveSpace()->getId();

        foreach ($sApps as $appId) {
            $result = Blyn::app()->getActiveApp()->doOperation(BSpaceEngine::ADDAppOPERATION, array('Space' => $spaceId, 'App' => $appId));
        }
    }

}

?>
